package com.qdxwx.manage.controller.book;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.area.AreaService;
import com.qdxwx.data.service.book.BookRequireService;
import com.qdxwx.data.service.book.BookService;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.area.AreaFull;
import com.qdxwx.models.book.Book;
import com.qdxwx.models.book.BookRequire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 样书申请Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/book/require")
public class BookRequireController extends BaseController {

    @Autowired
    private BookService bookService;
    @Autowired
    private BookRequireService bookRequireService;
    @Autowired
    private AreaService areaService;

    @Permissions("require:list")
    @GetMapping("/list.html")
    public String list() {
        return "book/book-require-list";
    }

    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        String searchKey = TKString.trim(request.getParameter("searchKey"));
        BookRequire bookRequire = new BookRequire();
        if (TKString.isNotEmpty(searchKey)) {
            if (TKString.isMobile(searchKey)) {
                bookRequire.setMobile(searchKey);
            } else {
                bookRequire.setSchoolName(searchKey);
            }
        }
        bookRequire.setStatus(TKConvert.toInteger(request.getParameter("status"), null));
        List<BookRequire> list = bookRequireService.getList(bookRequire, PageRequest.form(request));
        int count = bookRequireService.count(bookRequire);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(count);
        return rspData;
    }

    @GetMapping("/view.html")
    public String view(HttpServletRequest request, ModelMap mmap) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        if (id < 1) return error404;
        BookRequire bookRequire = bookRequireService.get(id);
        if (bookRequire == null) return error404;
        Book book = bookService.get(bookRequire.getBookId());
        StringBuffer address = new StringBuffer();
        AreaFull areaFull = areaService.getAreaFull(bookRequire.getAreaId());
        if(areaFull != null){
            address.append(areaService.get(areaFull.getProvinceId()).getName());
            address.append(areaService.get(areaFull.getCityId()).getName());
            address.append(areaService.get(areaFull.getCountyId()).getName());
        }
        address.append(bookRequire.getAddress());
        mmap.put("book", book);
        mmap.put("address", address.toString());
        mmap.put("bookRequire", bookRequire);
        return "book/book-require-view";
    }

    @PostMapping("/edit.json")
    @ResponseBody
    public AjaxResult edit(HttpServletRequest request, ModelMap mmap) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        String remark = TKString.trim(request.getParameter("remark"));
        if (id < 1) {
            return error("参数异常，id=" + id);
        }
        if (TKString.isEmpty(remark)) return error("请输入备注信息");
        BookRequire bookRequire = bookRequireService.get(id);
        if (bookRequire == null) {
            return error("找不到指定信息，id=" + id);
        }
        Book book = bookService.get(bookRequire.getBookId());
        if (book == null) {
            return error("找不到指定书籍，bookId=" + bookRequire.getBookId());
        }
        bookRequire.setStatus(1);
        bookRequire.setRemark(remark);
        return toAjax(bookRequireService.update(bookRequire));
    }
}
